//
//  ViewController.m
//  waterfall-test
//
//  Created by Song on 2018/6/20.
//  Copyright © 2018年 Song. All rights reserved.
//

//  九宫格

#import "ViewController.h"

@interface ViewController () <UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 需要看九宫格就打开
    [self jiugongge];
}

// 最基本最简单的九宫格
-(void)jiugongge {
    // 创建一个layout布局类
    UICollectionViewFlowLayout *layoutgg = [[UICollectionViewFlowLayout alloc] init];
    layoutgg.scrollDirection = UICollectionViewScrollDirectionVertical; // 垂直流布局
    //    layoutgg.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 水平流布局（流式布局） 设置为水平布局，则会在一列充满后，进行第二列的布局，
    layoutgg.itemSize = CGSizeMake(100, 100); // 每个item的大小
    
    // 通过布局策略layoutgg来创建 collectionview
    UICollectionView *collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 400) collectionViewLayout:layoutgg];
    
    // 代理
    collect.delegate = self;
    collect.dataSource = self;
    
    // 注册item类型 这里使用系统类型
    [collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellid"];
    
    [self.view addSubview:collect];
}

#pragma mark - 数据源
// 分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}
// 每个分区的item个数 必须实现
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}
// 每个item属性 必须实现
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    return cell;
}
//// 某个item可否移动
//-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
//    return YES;
//}
//-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
//    NSLog(@" - move - ");
//}

#pragma mark - 代理方法
// 点击每个item
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@",indexPath);
}
// 长按显示菜单
-(BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 这个方法用于设置要展示的菜单选项
-(BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return YES;
}
// 用于实现点击菜单按钮后的触发方法,通过测试，只有copy，cut和paste三个方法可以使用
-(void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    NSLog(@"%@",NSStringFromSelector(action));
}
@end
